package com.pushmessgae.server;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.pushmessgae.server.config.SysConfig;
import com.pushmessgae.server.log.Log;
import com.pushmessgae.server.util.SocketUtil;

public class Server implements InputListener {

	private static final String TAG = Server.class.getSimpleName();
	
	private boolean flag;

	private ServerSocket serverSocket;

	private ServerAccpet accpet;

	private ServerSender serverSender;
	
	private ClientContainer clientContainer = new ClientContainer();

	/**
	 * 启动服务器
	 * 
	 * @return
	 */
	public boolean start() {
		flag = true;
		accpet = new ServerAccpet(SysConfig.getInstance().getInt("server.accpet.port"));
		serverSender = new ServerSender();
		Log.d(TAG , "初始化服务器配置");
		new Thread(accpet).start();
		new Thread(serverSender).start();
		return true;
	}

	/**
	 * 停止服务器
	 * 
	 * @return
	 */
	public boolean stop() {
		flag = false;
		try {
//			new Socket("127.0.0.1", SysConfig.getInstance().getInt("server.accpet.port"));
			if(null != serverSocket && !serverSocket.isClosed()){
				serverSocket.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		accpet = null;
		serverSocket = null;
		serverSender = null;
		return true;
	}
	
	public boolean isStart(){
		return flag;
	}
	
	public boolean isStop(){
		return !flag;
	}

	/**
	 * 新的客户端连接
	 * 
	 * @param socket
	 * @throws IOException
	 */
	public void connectClient(Socket socket) throws IOException {
		Log.d(TAG , "新的客户端连接请求...(" + SocketUtil.getSocketAddress(socket)+ ")");
		final ClientThread clientThread = new ClientThread(socket);
		clientThread.start();
		clientContainer.addClient(clientThread);
	}

	public ClientContainer getClientContainer() {
		return clientContainer;
	}


	/**
	 * 服务器监听来自客户端的连接
	 * 
	 * @author sony
	 * 
	 */
	private final class ServerAccpet implements Runnable {

		private int port;

		public ServerAccpet(int port) {
			this.port = port;
		}

		@Override
		public void run() {
			try {
				Log.d(TAG , "服务器端口监听线程开始启动...");
				serverSocket = new ServerSocket(port);
				Log.d(TAG , "服务器开始端口监听：" + port);
				Socket client = null;
				while (flag) {
					final int maxaccpet = SysConfig.getInstance().getInt("server.maxconnect");
					if (clientContainer.getSize() <= maxaccpet && !serverSocket.isClosed()) {
						client = serverSocket.accept();
						connectClient(client);
					} else {
						flag = false;
						Log.d(TAG , "");
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				Log.d(TAG , "服务器关闭端口监听：" + port);
			}
		}

	}

	/**
	 * 向客户端发消息
	 * 
	 * @author oyf_feng
	 * 
	 */
	private final class ServerSender implements Runnable {

		// private ver

		@Override
		public void run() {
			Log.d(TAG , "向客户端发消息线程启动...");
		}

		public boolean send(DataInputStream in, String address) {
			return true;
		}

		public boolean sendAll(DataInputStream in) {
			return true;
		}

		public boolean sendAll(DataInputStream in, boolean synchroniz) {
			return true;
		}

	}

	@Override
	public void receive(byte[] bs) throws IOException {
		SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss");
		final String time = format.format(new Date());
		final File file = new File("d:\\" + time + ".txt");
		FileOutputStream outputStream = new FileOutputStream(file);
		outputStream.write(bs);
		outputStream.flush();
		outputStream.close();
		
	}

}
